/******************** 
	作用:公共功能
	作者:蔡俊雄
	版本:V1.0
	时间:2015-07-08
********************/

//用于当输入框的值发生改变时调用
$.event.special.valuechange = {
	teardown: function(namespaces) {
		$(this).unbind('.valuechange');
	},
	handler: function(e) {
		$.event.special.valuechange.triggerChanged($(this));
	},
	add: function(obj) {
		$(this).on('keyup.valuechange cut.valuechange paste.valuechange input.valuechange propertychange.valuechange', obj.selector, $.event.special.valuechange.handler);
	},
	triggerChanged: function(element) {
		var tagName = element[0].tagName.toUpperCase();
		//			var current = (tagName === 'TEXTAREA' || tagName === 'DIV' || tagName === 'SPAN') ? element.html() : element.val(),
		var current = (tagName === 'DIV' || tagName === 'SPAN') ? element.html() : element.val(),
			previous = typeof element.data('previous') === 'undefined' ? element[0].defaultValue : element.data('previous');
		if (current !== previous) {
			element.trigger('valuechange', [element.data('previous')]);
			element.data('previous', current);
		}
	}
};

(function(window) {
	function General() {}
	//特殊字符
	General.specialString = "null###yedadou###null";
	//-----------------遮罩-----------------

	//用于保存遮罩层
	General.maskBg = null;

	/**
	 * 显示遮罩
	 */
	General.showMask = function(info) {
		try {
			if (General.maskBg == null) {
				General.maskBg = $("<div class='mask-bg'></div>").appendTo($("body"));
			}
			General.maskBg.show();
			if (info != undefined && info != null) {
				Common.showLoading(info);
			}
		} catch (e) {
			Common.log(e);
		}
	}

	/**
	 * 显示遮罩
	 */
	General.hideMask = function() {
		try {
			if (General.maskBg != null) {
				General.maskBg.hide();
			}
		} catch (e) {
			Common.log(e);
		}
	}

	//-----------------停止事件冒泡-----------------
	/**
	 * 停止事件冒泡
	 * @param {Object} e 事件
	 */
	General.stopEvent = function(e) {
		e.stopImmediatePropagation();
		e.stopPropagation();
		e.preventDefault();
	}

	//-----------------弹出框-----------------
	/**
	 * 弹出框
	 * @param {String} info 弹出内容
	 */
	General.alert = function(info) {
		alert(info);
	}

	//-----------------关闭编辑窗口-----------------
	/**
	 * 关闭编辑窗口
	 * @param {Object} options 用作扩展用
	 */
	General.closeEditWindow = function(options, dialogId) {
		if (dialogId == undefined || dialogId == null)
			$("#dialog-edit").dialog("close");
		else
			$("#" + dialogId).dialog("close");
	}

	//-----------------表格中的复选框操作-----------------
	/**
	 * 初始化表格中的复选框事件监听
	 */
	General.initCheckbox = function() {
		//点击"全选"复选框
		$("#check-all").on("click", function(e) {
			$('input[type=checkbox][name=check-row]').prop('checked', $(this).prop('checked'));
			General.updateCheckedIds();
		});
		$("input[type=checkbox][name=check-row]").on("click", function(e) {
			General.updateCheckedIds();
		});
		try {
			if ($("#addToGroup").length > 0) {
				$("#addToGroup").attr("disabled", "disabled");
			}
		} catch (e) {
			Common.log(e);
		}
	}

	/**
	 * 更新已被选中的行ID
	 */
	General.updateCheckedIds = function() {
		var ids = [];
		var id;
		$('input[type=checkbox][name=check-row]:checked').each(function(index, element) {
			id = $(this).closest("tr").attr("data-id");
			ids.push(id);
		});
		try {
			if ($("#addToGroup").length > 0) {
				if (ids.length > 0) {
					$("#addToGroup").removeAttr("disabled");
				} else {
					$("#addToGroup").attr("disabled", "disabled");
				}
			}
		} catch (e) {
			Common.log(e);
		}
		$("#checkedIds").val(ids.join(","));
	}
	//-----------------扩展对话框-----------------
	General.hasExtendDialogTitle = false;
	/**
	 * 扩展对话框
	 */
	General.extendDialog = function() {
		General.hasExtendDialogTitle = true;
		$.widget("ui.dialog", $.extend({}, $.ui.dialog.prototype, {
			_title: function(title) {
				var $title = this.options.title || ' ';
				if (("title_html" in this.options) && this.options.title_html == true){
					title.html($title);
				}else{
					title.text($title);
				}
			}
		}));
	}

	//-----------------ifame加载-----------------
	/**
	 * 是否已经注册过加载事件(true:已注册 false:未注册)
	 */
	General.isInitLoadIframe = false;
	/**
	 * 对话框中的iframe的ID
	 */
	General.dialogIframeId = "top-container";

	/**
	 * 弹出窗口的ID
	 */
	General.dialogId = "modalDialog";

	/**
	 * 调节弹出窗口的高度
	 */
	General.isAdjustDialogHeight = false;

	/**
	 * 注册ifame加载完成事件
	 * @param {String} dialogIframeId iframe的ID
	 * @param {Function} completeFunction iframe中的页面加载完成后执行的操作
	 */
	General.initLoadIframe = function(dialogIframeId, completeFunction) {
		General.isInitLoadIframe = true;
		try {
			//			
			var oFrm = document.getElementById(dialogIframeId);
			if (oFrm.onreadystatechange != undefined) {
				oFrm.onreadystatechange = function() {
					if (this.readyState && this.readyState != 'complete') return;
					else{
						completeFunction();
					}
				}
			} else {
				oFrm.onload = function() {
					if (this.readyState && this.readyState != 'complete') return;
					else{
						completeFunction();
					}
				}
			}
		} catch (e) {
			Common.log(e);
		}
	}

	/**
	 * 显示url地址对应的页面
	 * @param {String} url 网页地址
	 */
	General.showIframePage = function(url) {
		//监听点击事件
		if (url != "" && url != "#") {
			//链接不为空
			url = Common.addUrlParameters(url, {
				"dt": Math.random() + ""
			});
			$("#" + General.dialogIframeId).attr("src", url);
			General.showIframeLoading();
			if (!General.isInitLoadIframe)
				General.initLoadIframe(General.dialogIframeId, General.hideIframeLoading);
		}
	};

	/**
	 * 获取iframe页面中内容的高度
	 */
	General.getIframeHeight = function() {
		var result = 0;
		try {
			var iframe = document.getElementById(General.dialogIframeId);
			if (iframe.attachEvent) {
				result = iframe.contentWindow.document.documentElement.scrollHeight;
			} else {
				result = iframe.contentDocument.body.scrollHeight;
			}
		} catch (e) {

		}
		return result;
	}

	/**
	 * 调节iframe的高度以适应内容的高度
	 */
	General.adjustDialogHeight = function() {
		var maxPercent = 0.92; //最大高度百分比
		var widgetHeight = 90; //要减去的高度
		var innerHeight = General.getIframeHeight();
		var maxHeight = $(window).height() * maxPercent; //获取窗口的高度
		var currentHeight = $("#" + General.dialogId).dialog("option", "height"); //当前高度
		innerHeight += widgetHeight;

		console.log(innerHeight, currentHeight, maxHeight)
			//		if(innerHeight<currentHeight){
			//			$("#" + General.dialogId).dialog("option", "height", innerHeight);
			//		}else {
		if (innerHeight < maxHeight) {
			$("#" + General.dialogId).dialog("option", "height", innerHeight);
		} else {
			$("#" + General.dialogId).dialog("option", "height", maxHeight);
		}
		//		}
	}

	/**
	 * 隐藏页面的加载状态(用于iframe中加载页面)
	 */
	General.hideIframeLoading = function() {
		$("#" + General.dialogIframeId).siblings(".loading-bg").addClass("hide");
		//调节iframe的高度
		if (General.isAdjustDialogHeight) {
			General.adjustDialogHeight();
		}
	};

	/**
	 * 显示页面的加载状态(用于iframe中加载页面)
	 */
	General.showIframeLoading = function() {
		$("#" + General.dialogIframeId).siblings(".loading-bg").removeClass("hide");
	};

	/**
	 * 隐藏页面的加载状态(用于在当前页面中通过ajax加载数据)
	 */
	General.hideDialogLoading = function() {
		$("#dialog-edit .loading-bg").addClass("hide");
		$("#dialog-edit").siblings(".ui-dialog-buttonpane").find("button").removeAttr("disabled");
	};

	/**
	 * 显示页面的加载状态(用于在当前页面中通过ajax加载数据)
	 */
	General.showDialogLoading = function(options) {
		if ($("#dialog-edit .loading-bg").length <= 0) {
			var loadingDiv = '<div class="loading-bg"><div class="loading-animation"><i class="fa fa-spinner fa-spin"></i></div></div>';
			$(loadingDiv).appendTo("#dialog-edit");
		}
		$("#dialog-edit .loading-bg").removeClass("hide");
		//默认禁用所有按钮,如果设置disableButton参数则只禁用disableButton指定的按钮
		if (options && options.disableButton) {
			$("#dialog-edit").siblings(".ui-dialog-buttonpane").find(options.disableButton).attr("disabled", "disabled");
		} else {
			$("#dialog-edit").siblings(".ui-dialog-buttonpane").find("button").attr("disabled", "disabled");
		}
	};

	/**
	 * 关闭弹出窗口
	 */
	General.closeWindow = function(selector) {
		try {
			if (selector)
				$(selector).dialog("close");
			else
				General.closeDialog();
		} catch (e) {
			try {
				General.closeDialog();
			} catch (e2) {}
		}
	};

	/**
	 * 关闭弹出窗口并刷新当前页面
	 */
	General.closeAndRefresh = function(selector) {
		General.closeWindow(selector);
		window.location.reload();
	};

	/**
	 * 关闭弹出窗口
	 */
	General.closeDialog = function() {
		General.closeWindow("#" + General.dialogId);
	};

	/**
	 * 显示弹出窗口
	 * @param {String} title 弹出窗口的标题
	 * @param {String} url 页面地址
	 */
	General.showDialog = function(title, url) {
		var height = $(window).height(); //获取窗口的高度
		height = height * 0.9;
		$("#" + General.dialogId).removeClass('hide').dialog({
			resizable: false,
			width: '90%',
			height: height,
			modal: true,
			closeText: "关闭",
			title: "<div class='widget-header'><h4 class='smaller'> " + title + "</h4></div>",
			title_html: true,
			buttons: []
		});
		General.showIframePage(url);
	}

	/**
	 * 显示弹出窗口(使用宽度和高度)
	 * @param {String} title 弹出窗口的标题
	 * @param {String} url 页面地址
	 * @param {Number} width 对话框宽度
	 * @param {Number} height 对话框高度(如果高度设置为0表示采用当前页面90%高度)
	 */
	General.showDialogWidthHeight = function(title, url, width, height) {
		if (height == 0) {
			height = $(window).height(); //获取窗口的高度
			height = height * 0.9;
		}
		$("#" + General.dialogId).removeClass('hide').dialog({
			resizable: false,
			width: width,
			height: height,
			modal: true,
			closeText: "关闭",
			title: "<div class='widget-header'><h4 class='smaller'> " + title + "</h4></div>",
			title_html: true,
			buttons: []
		});
		General.showIframePage(url);
	}

	/**
	 * 显示弹出窗口(有关闭按钮,使用宽度和高度)
	 * @param {String} title 弹出窗口的标题
	 * @param {String} url 页面地址
	 * @param {Number} width 对话框宽度
	 * @param {Number} height 对话框高度(如果高度设置为0表示采用当前页面90%高度)
	 */
	General.showDialogWidthHeightClose = function(title, url, width, height) {
		General.isAdjustDialogHeight = true;
		if (height == 0) {
			height = $(window).height(); //获取窗口的高度
			height = height * 0.9;
		}
		$("#" + General.dialogId).removeClass('hide').dialog({
			resizable: false,
			width: width,
			height: height,
			modal: true,
			closeText: "关闭",
			title: "<div class='widget-header'><h4 class='smaller'> " + title + "</h4></div>",
			title_html: true,
			buttons: [{
				html: "<i class='ace-icon fa fa-times'></i> 关闭",
				"class": "btn btn-danger btn-minier",
				click: function() {
					$(this).dialog("close");
				}
			}]
		});
		General.showIframePage(url);
	}

	/**
	 * 刷新页面
	 */
	General.refresh = function() {
		window.location.reload();
	};

	/**
	 * 初始化页面中所有自动跳转的下拉列表,切换值时自动跳转到相应的url,如果值为#则不跳转
	 * @param {String} selector 选择器
	 */
	General.initAutoPostSelect = function(selector) {
		if (selector == undefined || selector == null)
			selector = "select.auto-jump";
		$(selector).on('change', function(e) {
			var url = $(this).find("option:selected").attr("data-href");
			if (url != "#" && url != "") {
				//判断是否有附加参数
				var ids = $(this).attr("data-addition");
				if (ids) {
					var obj;
					$.each(ids.split(","), function(index, value) {
						obj = {};
						var key = value;
						if (value.indexOf("#") == 0) {
							key = value.replace("#", "");
						} else if (value.indexOf(".") == 0) {
							key = value.replace(".", "");
						}
						obj[key] = General.getValue(value);
						url = Common.addUrlParameters(url, obj);
					});
				}
				window.location.href = url;
			}
		});
	};

	/**
	 * 获取选择器对应的值
	 * @param {String} selector 选择器
	 */
	General.getValue = function(selector) {
		//判断选择符是否以"#或.开始"
		if (selector.indexOf("#") != 0) {
			selector = "#" + selector;
		}
		var type = "value"; //设置值的方式
		var tagName = $(selector)[0].tagName.toLowerCase();
		if (tagName != "input") {
			type = "html";
		}
		//获取初始值
		var result = "";
		if (type == "value") {
			result = $(selector).val();
		} else {
			result = $(selector).html();
		}
		return result;
	};

	/**
	 * 弹出服务器提示
	 * @param {String} msg 服务器提示
	 * @param {String} url 点确定后要跳转的地址,为空则不进行操作
	 * @param {Object} win 当前窗口
	 */
	General.showServerMessage = function(msg, url, win) {
		if (!General.hasExtendDialogTitle)
			General.extendDialog(); //扩展对话框
		if ($("#dialog-tips").length > 0) {
			$("#dialog-tips").remove();
		}
		var str = "<div id='dialog-tips' class='dialog-tips hide'><div class='dialog-content-tip center p10'><div class='middle-outer'><div class='middle-inner'>" + msg + "</div></div></div></div>";
		$(str).appendTo($("body"));
		$("#dialog-tips").removeClass('hide').dialog({
			resizable: false,
			width: '360',
			modal: true,
			title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-info-circle text-info'></i> 提示</h4></div>",
			title_html: true,
			closeText: "关闭",
			close: function(event, ui) {
				if (url) {
					if (win)
						win.location.href = url;
					else
						window.location.href = url;
				}
			},
			buttons: [{
				html: "确定",
				"class": "btn btn-info btn-minier",
				click: function() {
					$(this).dialog("close");

				}
			}]
		});
	};

	//-----------------不同类型的对话框-----------------
	/**
	 * 弹出框
	 * @param {String} msg 弹出信息
	 * @param {String} title 标题
	 * @param {Function} okFun 点击确定后的操作
	 * @param {String} data 数据
	 */
	General.alert = function(msg, title, okFun, data) {
		if (!General.hasExtendDialogTitle)
			General.extendDialog(); //扩展对话框
		if ($("#dialog-alert").length > 0) {
			$("#dialog-alert").remove();
		}
		var titleStr = "";
		if (!title) {
			title = "提示";
		}
		//		if (title == "") {
		//			titleStr = "<div class='widget-header'><h4 class='smaller'> " + title + "</h4></div>";
		//		} else {
		titleStr = "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-info-circle text-info'></i> " + title + "</h4></div>";
		//		}
		var str = "<div id='dialog-alert' class='dialog-alert hide'><div class='dialog-content-tip center p10'><div class='middle-outer'><div class='middle-inner'>" + msg + "</div></div></div></div>";
		$(str).appendTo($("body"));
		$("#dialog-alert").removeClass('hide').dialog({
			resizable: false,
			width: '400',
			modal: true,
			title: titleStr,
			title_html: true,
			closeText: "关闭",
			close: function(event, ui) {
				if (okFun) {
					if (data)
						okFun(data);
					else
						okFun();
				}
			},
			buttons: [{
				html: "确定",
				"class": "btn btn-info btn-minier",
				click: function() {
					$(this).dialog("close");
				}
			}]
		});
	};

	/**
	 * 确认框
	 * @param {String} msg 弹出信息
	 * @param {String} title 标题
	 * @param {Function} okFun 点击确定后的操作
	 * @param {Function} cancelFun 点击确定后的操作
	 * @param {String} data 数据
	 */
	General.confirm = function(msg, title, okFun, cancelFun, data) {
		if (!General.hasExtendDialogTitle)
			General.extendDialog(); //扩展对话框
		if ($("#dialog-confirm").length > 0) {
			$("#dialog-confirm").remove();
		}
		var titleStr = "";
		if (!title) {
			title = "提示";
		}
		//		if (title == "") {
		//			titleStr = "<div class='widget-header'><h4 class='smaller'> " + title + "</h4></div>";
		//		} else {
		titleStr = "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-info-circle text-info'></i> " + title + "</h4></div>";
		//		}
		var str = "<div id='dialog-confirm' class='dialog-confirm hide'><div class='dialog-content-tip center p10'><div class='middle-outer'><div class='middle-inner'>" + msg + "</div></div></div></div>";
		$(str).appendTo($("body"));
		$("#dialog-confirm").removeClass('hide').dialog({
			resizable: false,
			width: '400',
			modal: true,
			title: titleStr,
			title_html: true,
			closeText: "关闭",
			close: function(event, ui) {},
			buttons: [{
				html: "<i class='ace-icon fa fa-check'></i> 确定",
				"class": "btn btn-info btn-minier btn-space-right",
				click: function() {
					$(this).dialog("close");
					if (okFun) {
						if (data)
							okFun(data);
						else
							okFun();
					}

				}
			}, {
				html: "<i class='ace-icon fa fa-times'></i> 取消",
				"class": "btn btn-danger btn-minier",
				click: function() {
					$(this).dialog("close");
					if (cancelFun) {
						if (data)
							cancelFun(data);
						else
							cancelFun();
					}
				}
			}]
		});
	};

	/**
	 * 将form中的值转换为键值对
	 * @param {String} formSelector 表单选择
	 */
	General.getFormJson = function(formSelector) {
		var o = {};
		var a = $(formSelector).serializeArray();
		$.each(a, function() {
			if (o[this.name] !== undefined) {
				if (!o[this.name].push) {
					o[this.name] = [o[this.name]];
				}
				o[this.name].push(this.value || '');
			} else {
				o[this.name] = this.value || '';
			}
		});
		return o;
	}
	/**
	 * 判断数组中是否存在要查找的值
	 * @param {Array} arr 目标数组
	 * @param {String} str 要查找的字符串
	 */
	General.indexOf = function(arr,str) {
		var result=-1;
		$.each(arr,function(index,value) {
			if(value==str){
				result=index;
				return false;
			}
		});
		return result;
	}
	//--------------------------------
	window.General = General;
	//--------------------------------
})(window);

(function(window) {
	function Common() {}

	//-----------------配置设置(START)-----------------

	//-----------------配置设置(END)-----------------

	//用于记录提示进度信息
	Common.tipId = null;

	//动画间隔时间(毫秒)
	Common.interval = 300;

	//动画当前步数
	Common.step = 0;

	//动画最大步数
	Common.maxStep = 6;
	//-----------------提示功能-----------------
	/**
	 * 开始动画
	 */
	Common.startAnimation = function() {
		Common.stopAnimation();
		Common.tipId = setInterval(Common.showCurrentStep, Common.interval);
	}

	/**
	 * 停止动画
	 */
	Common.stopAnimation = function() {
		if (Common.tipId != null) {
			clearInterval(Common.tipId);
			Common.tipId = null;
			Common.step = 0; //动画当前步数
			Common.showCurrentStep();
		}
	}

	/**
	 * 显示当前进度
	 */
	Common.showCurrentStep = function() {
		var sign = ".";
		var info = "";
		Common.step++;
		if (Common.step > Common.maxStep)
			Common.step = 0;
		for (var i = 0; i < Common.step; i++) {
			info += sign;
		}
		$("#operation-tip span:eq(1)").html(info);
	}

	/**
	 * 显示操作正在进行中的信息
	 * @param {String} info 提示信息
	 * @param {Object} options 用作扩展用
	 */
	Common.showLoading = function(info, options) {
		if (info == null) {

		} else {
			$("#operation-tip").removeClass('hide').find("span:eq(0)").html(info);
			Common.startAnimation();
		}
	}

	/**
	 * 隐藏操作正在进行中的信息
	 * @param {Object} options 用作扩展用
	 */
	Common.hideLoading = function(options) {
		Common.stopAnimation();
		$("#operation-tip").addClass('hide');
	}


	/**
	 * 关闭编辑窗口
	 * @param {Object} options 用作扩展用
	 */
	Common.closeEditWindow = function(options) {
		$("#dialog-edit").dialog("close");
	}

	//-----------------ajax-----------------
	/**
	 * ajax提交
	 * @param {Object} options 初始配置参数
	 * @param {Function} preAction 进行提交前的准备工作的函数
	 * @param {String} url 提交的地址
	 * @param {Object} data 提交的数据
	 * @param {String} tip 提示信息
	 * @param {Function} successFun 成功时运行的函数
	 * @param {Function} errorFun 失败时运行的函数
	 * @param {String} method 提交时的方法
	 */
	Common.ajaxPost = function(options, preAction, url, data, tip, successFun, errorFun,method) {
		Common.showLoading(tip);
		if (preAction != null) {
			preAction(options);
		}
		if(method){
			
		}else{
			method='POST';
		}
		$.ajax({
			type: method,
			url: url,
			cache: false,
			data: data,
			success: function(data, status) {
				Common.hideLoading();
				try {
					if (successFun != null)
						successFun(options, data, status);
				} catch (e) {}

			},
			error: function() {
				Common.hideLoading();
				try {
					if (errorFun != null)
						errorFun(options);
				} catch (e) {}
			}
		});
	}

	/**
	 * ajax提交,根据服务器的返回值进行不同的操作
	 * @param {String} url 提交的地址
	 * @param {Object} data 提交的数据
	 * @param {String} tip 提交等待时的提示信息
	 * @param {Object} win 当前窗口
	 * @param {Object} maskWin 被遮罩的窗口
	 */
	Common.ajaxPostWantResult = function(url, data, tip, win, maskWin) {
		Common.showLoading(tip);
		$.ajax({
			type: 'POST',
			url: url,
			cache: false,
			data: data,
			success: function(data, status) {
				try {
					if (maskWin)
						maskWin.General.hideMask();
				} catch (e2) {

				}
				Common.hideLoading();
				try {
					data = $.parseJSON(data); //获取json格式的数据
					var msg = data["msg"] + data["sub_msg"]; //提示
					General.showServerMessage(msg, data["url"], win); //弹出服务器执行结果
				} catch (e) {

				}
			},
			error: function() {
				try {
					if (maskWin)
						maskWin.General.hideMask();
				} catch (e2) {

				}
				Common.hideLoading();
			}
		});
	}

	/**
	 * 为url地址中添加参数
	 * @param {Object} url url地址
	 */
	Common.addUrlParameters = function(url, params) {
		try {
			var oldParams = Common.getUrlParameters(url);
			params = $.extend(oldParams, params);
			var urlParams = $.param(params); //生成新的参数
			//截取?号前的字符串
			var index = url.indexOf("?");
			if (index != -1) {
				url = url.substring(0, index);
			}
			url += "?" + urlParams;
		} catch (e) {
			Common.log(e);
		}
		return url;
	}

	/**
	 * 获取url地址中包含的参数
	 * @param {Object} url url地址
	 */
	Common.getUrlParameters = function(url) {
		var vars = {};
		try {
			var parts = url.replace(/[?&]+([^=&]+)=([^&]*)/gi,
				function(m, key, value) {
					vars[key] = value;
				});
		} catch (e) {
			Common.log(e);
		}
		return vars;
	}

	/**
	 * 记录信息
	 * @param {Object} data 要记录的数据
	 */
	Common.log = function(data) {
		try {
			if (data == undefined || data == null) {} else if (data.stack != undefined)
				console.log(data.stack);
			else
				console.log(data);
		} catch (e) {

		}
	}

	//--------------------------------
	if (window.Common == undefined) {
		if (window.top.Common == undefined)
			window.Common = Common;
		else
			window.Common = window.top.Common;
	}
	//--------------------------------
})(window);

//--------------------------------全局设置
$(function() {
	//-----------------限制只能输入数字-----------------
	$("body").delegate("input[data-onlyNumber='true']", 'keyup blur paste', function(e) {
		if (e.type == "focusout" || e.type == "paste") {
			checkIsNumber($(e.target), true); //检查输入值是否为数字
		} else {
			checkIsNumber($(e.target)); //检查输入值是否为数字
		}
	});
	/**
	 * 检查输入值是否为数字
	 * @param {String} element 目标元素
	 * @param {Boolean} checkValid 检查数字是否有效
	 */
	function checkIsNumber(element, checkValid) {
		var value = element.val().replace(/[^.0123456789]/g, '');
		if (checkValid) {
			var re = /^[1-9]\d*|^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$/; //非负整数+小数点  							
			if (!re.test(value) && value != "") {
				$(element).val(0);
			} else {
				element.val(value);
			}
		} else {
			element.val(value);
		}
	};
	
	
	//判断是否有自动提交的表单
	if ($("form[data-submit=auto]").length > 0) {
		//点击"取消"按钮关闭窗口
		$("form[data-submit=auto] button[data-action=close]").on("click", function(e) {
			General.stopEvent(e);
			//			console.log(window.parent);
			if (window.parent)
				window.parent.General.closeDialog();
			else
				window.close();
		});
		//点击"取消"按钮返回上一页
		$("form[data-submit=auto] button[data-action=return]").on("click", function(e) {
			General.stopEvent(e);
			window.history.go(-1);
		});
		//点击"有提交动作"的按钮提交数据
		$("form[data-submit=auto] button[data-action=submit]").on("click", function(e) {
			General.stopEvent(e);
			//获取提交地址
			var url = $(this).attr("data-href");
			var beforeAction = $(this).attr("data-before-submit");
			var refreshTarget = $(this).attr("data-refresh");
			var ignoreKey = $(this).attr("data-ignore"); //忽略的key
			var targetWin = window;
			if (refreshTarget == "parent")
				targetWin = window.parent;
			else if (refreshTarget == "parent.parent")
				targetWin = window.parent.parent;
			var mask = $(this).attr("data-mask");
			var maskWin = null;
			if (mask != undefined) {
				if (mask == "parent") {
					maskWin = window.parent;
					window.parent.General.showMask();
				} else if (mask == "parent.parent") {
					maskWin = window.parent.parent;
					window.parent.parent.General.showMask();
				} else {
					maskWin = window;
					General.showMask();
				}
			} else {

			}
			//判断是否有编辑器需要同步
			if (window.editorLists) {
				$.each(window.editorLists, function(index, editor) {
					try {
						editor.sync();
					} catch (e) {

					}
				});
			}
			if (beforeAction) {
				//提交前的操作
				try {
					window[beforeAction]();
				} catch (e) {

				}
				//				return false;
			}
			//提交数据
			//			var str=$(this).closest("form").serialize();
			var data = General.getFormJson($(this).closest("form"));

			try {
				if (ignoreKey) {
					var keyArray = ignoreKey.split(":");
					data[keyArray[0]] = window[keyArray[1]];
				}
			} catch (e) {

			}
			//			alert(data[ignoreKey]);
			//			alert($("#"+ignoreKey).val());
			//			data[ignoreKey]=$("#"+ignoreKey).val();
//						console.log(data);
			Common.ajaxPostWantResult(url, data, "", targetWin, maskWin);
			//			console.log(str);
			//			console.log(str2);
			//			alert($(this).serialize());
			//			alert(url);
			//提交数据
			//			window.parent.General.closeAndRefresh();//关闭弹出窗口并刷新父级页面
		});
	}

	//判断是否有分页页码输入框和转到按钮
	if ($("[name=pageNumber]").length > 0 && $("[name=submitPage]").length > 0) {
		//		alert("分页页码输入框限制");
	}

	
	


});